package application;

import java.util.ArrayList;

import javafx.event.EventHandler;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;

public class GraphicsEngine {
	public Canvas canvas;

	public double CANVAS_WIDTH, CANVAS_HEIGHT;
	public double SCALE_WIDTH, SCALE_HEIGHT;
	public double SCALE_WIDTH_OFFSET, SCALE_HEIGHT_OFFSET;

	public GraphicsEngine(double cHeight, double cWidth) {
		CANVAS_WIDTH = cWidth;
		CANVAS_HEIGHT = cHeight;

		canvas = new Canvas(CANVAS_WIDTH, CANVAS_HEIGHT);
		canvas.setLayoutX(0.0D);
        canvas.setLayoutY(0.0D);
	}

	public void setScaling(double mHeight, double mWidth) {
		SCALE_WIDTH = mWidth * .95;
		SCALE_HEIGHT = mHeight * .95;
		SCALE_WIDTH_OFFSET = CANVAS_WIDTH*.025;
		SCALE_HEIGHT_OFFSET = CANVAS_HEIGHT*.025;
	}

	public void attachHandler(EventHandler<MouseEvent> me) {
		canvas.setOnMouseClicked(me);
	}

	public void clear() {
		GraphicsContext g = canvas.getGraphicsContext2D();
		g.setFill(Color.WHITE);
		g.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
	}
	
	public Canvas getCanvas() { return this.canvas; }

	@SuppressWarnings("unchecked")
	public void render(ArrayList<MapElement>... params) {
		GraphicsContext g = canvas.getGraphicsContext2D();
		try {
			for(ArrayList<MapElement> mapElements : params)
				for(MapElement m: mapElements)
					m.drawElement(g);
		} catch(Exception ex) {
			System.out.println("ERROR: " + ex.getStackTrace());
		}
	}
}